﻿using System;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Lottery.Client.SL.Controls
{
    public partial class WinOpenInfo : UserControl
    {
        private BonusStatus _bonusStatus;
        private bool _bool;
        private DispatcherTimer _dispatcherTimer;
        private double _double1;
        private double _double2;
        private EventHandler _eventHandler1;
        private int _int1;
        private int _int2;
        private PlayDataInfo _playDataInfo;
        private Storyboard _storyboard;

        public WinOpenInfo()
        {
            _double1 = -1.0;
            _double2 = -1.0;
            InitializeComponent();
            base.Loaded += (Base_Loaded);
            SetPomponChildrenProperty();
            var rectangleGeometry = new RectangleGeometry();
            rectangleGeometry.RadiusX = (20.0);
            rectangleGeometry.RadiusY = (20.0);
            Rect rect = default(Rect);
            rect.Width = (pompon.Width);
            rect.Height = (pompon.Height);
            rectangleGeometry.Rect = (rect);
            pompon.Clip = (rectangleGeometry);
            pompon.Visibility = Visibility.Collapsed;
        }

        public BonusStatus CurrentBonusStatus
        {
            get { return _bonusStatus; }
            set
            {
                if (value != _bonusStatus)
                {
                    _bonusStatus = value;
                    if (_eventHandler1 != null)
                    {
                        _eventHandler1.Invoke(this, null);
                    }
                }
            }
        }

        public string WinNumber { get; set; }

        public event EventHandler BonusStatusChanged
        {
            add
            {
                EventHandler eventHandler = _eventHandler1;
                EventHandler eventHandler2;
                do
                {
                    eventHandler2 = eventHandler;
                    var eventHandler3 = (EventHandler) Delegate.Combine(eventHandler2, value);
                    eventHandler = Interlocked.CompareExchange(ref _eventHandler1, eventHandler3, eventHandler2);
                } while (eventHandler != eventHandler2);
            }
            remove
            {
                EventHandler eventHandler = _eventHandler1;
                EventHandler eventHandler2;
                do
                {
                    eventHandler2 = eventHandler;
                    var eventHandler3 = (EventHandler) Delegate.Remove(eventHandler2, value);
                    eventHandler = Interlocked.CompareExchange(ref _eventHandler1, eventHandler3, eventHandler2);
                } while (eventHandler != eventHandler2);
            }
        }

        public void Init(PlayDataInfo pdi)
        {
            _playDataInfo = pdi;
        }

        private void Base_Loaded(object obj, RoutedEventArgs routedEventArgs)
        {
            StoryBegin();
            CreateLastDataUIElement();
        }

        public void StoryBegin()
        {
            SetStoryboard();
            tbGameName.Text = (LotteryInfos.GetLotteryName(DataCache.CurrentPlayInfo.LotteryID));
            _dispatcherTimer.Start();
        }

        private void _dispatcherTimer_Tick(object obj, EventArgs eventArgs)
        {
            bool debug = true;
            if (debug)
            {
                if (_playDataInfo.LastPlayData == null)
                    _playDataInfo.LastPlayData = new PlayData {BonusNumber = "888"};
            }
            if (!debug)
            {
                if (_playDataInfo == null || _playDataInfo.LastPlayData == null || _playDataInfo.CurrentPlayData == null)
                {
                    CurrentBonusStatus = BonusStatus.NetStart;
                    return;
                }
                if (DateTime.Now < _playDataInfo.LastPlayData.StartTime)
                {
                    CurrentBonusStatus = BonusStatus.NetStart;
                    tbCurrentInfo.Text =
                        (string.Format("{0}期 距离StartTime时间还有{1}", _playDataInfo.LastPlayData.IsusesName,
                                       FormatTime(_playDataInfo.LastPlayData.StartTime - DateTime.Now)));
                    return;
                }
                if (DateTime.Now < _playDataInfo.LastPlayData.AheadEndTime)
                {
                    CurrentBonusStatus = BonusStatus.StartBet;
                    tbCurrentInfo.Text =
                        (string.Format("{0}期 距离AheadEndTime时间还有{1}", _playDataInfo.LastPlayData.IsusesName,
                                       FormatTime(_playDataInfo.LastPlayData.AheadEndTime - DateTime.Now)));
                    return;
                }
                if (DateTime.Now < _playDataInfo.LastPlayData.EndTime)
                {
                    CurrentBonusStatus = BonusStatus.EndBet;
                    tbCurrentInfo.Text =
                        (string.Format("{0}期 距离EndTime时间还有{1}", _playDataInfo.LastPlayData.IsusesName,
                                       FormatTime(_playDataInfo.LastPlayData.EndTime - DateTime.Now)));
                    return;
                }
            }
            if (string.IsNullOrEmpty(_playDataInfo.LastPlayData.BonusNumber))
            {
                tbLastInfo.Text = (string.Format("{0}", _playDataInfo.LastPlayData.IsusesName));
                if (!string.IsNullOrEmpty(WinNumber))
                {
                    WinNumber = "";
                    if (LoadData.Visibility != null)
                    {
                        LoadData.Visibility = Visibility.Visible;
                    }
                    if (pompon.Visibility != Visibility.Collapsed)
                    {
                        pompon.Visibility = Visibility.Collapsed;
                    }
                    for (int i = 0; i < pompon.Children.Count; i++)
                    {
                        var canvas = (Canvas) pompon.Children[i];
                        canvas.SetValue(Canvas.LeftProperty, -80.0);
                    }
                    SetStoryboard();
                }
            }
            else
            {
                //向右滚动到位效果
                if (string.IsNullOrEmpty(WinNumber))
                {
                    WinNumber = _playDataInfo.LastPlayData.BonusNumber;
                    tbLastInfo.Text = (string.Format("{0}", _playDataInfo.LastPlayData.IsusesName));
                    if (LoadData.Visibility != Visibility.Collapsed)
                    {
                        LoadData.Visibility = Visibility.Collapsed;
                    }
                    if (pompon.Visibility != null)
                    {
                        pompon.Visibility = Visibility.Visible;
                    }

                    _int1 = WinNumber.Length - 1;
                    _int2 = WinNumber.Length;
                    if (_int1 > 0)
                    {
                        SetStoryboard2(_int1);
                    }
                    CreateLastDataUIElement();
                }
            }
            if (!debug)
            {
                if (DateTime.Now < _playDataInfo.CurrentPlayData.StartTime)
                {
                    CurrentBonusStatus = BonusStatus.NetStart;
                    tbCurrentInfo.Text =
                        (string.Format("{0}期 距离StartTime时间还有{1}", _playDataInfo.CurrentPlayData.IsusesName,
                                       FormatTime(_playDataInfo.CurrentPlayData.StartTime - DateTime.Now)));
                    return;
                }
                if (DateTime.Now < _playDataInfo.CurrentPlayData.AheadEndTime)
                {
                    CurrentBonusStatus = BonusStatus.StartBet;
                    TimeSpan timeSpan = _playDataInfo.CurrentPlayData.AheadEndTime - DateTime.Now;
                    tbCurrentInfo.Text =
                        (string.Format("{0}期 距离AheadEndTime时间还有{1}", _playDataInfo.CurrentPlayData.IsusesName,
                                       FormatTime(timeSpan)));
                    double num = Math.Round(double.Parse(timeSpan.TotalSeconds.ToString()), 0);
                    if (DataCache.RealSound && num >= 0.0 && num < 8.0 && num%2.0 == 0.0)
                    {
                        msgCountdown.Stop();
                        msgCountdown.Play();
                        return;
                    }
                }
                else
                {
                    if (DateTime.Now < _playDataInfo.CurrentPlayData.EndTime)
                    {
                        CurrentBonusStatus = BonusStatus.EndBet;
                        tbCurrentInfo.Text =
                            (string.Format("{0}期 距离EndTime时间还有{1}", _playDataInfo.CurrentPlayData.IsusesName,
                                           FormatTime(_playDataInfo.CurrentPlayData.EndTime - DateTime.Now)));
                        return;
                    }
                    if (_eventHandler1 != null)
                    {
                        _eventHandler1.Invoke(this, null);
                    }
                }
            }
        }

        private void _dispatcherTimer_Tick1(object obj, EventArgs eventArgs)
        {
            if (_playDataInfo == null || _playDataInfo.LastPlayData == null || _playDataInfo.CurrentPlayData == null)
            {
                CurrentBonusStatus = BonusStatus.NetStart;
                return;
            }
            if (DateTime.Now < _playDataInfo.LastPlayData.StartTime)
            {
                CurrentBonusStatus = BonusStatus.NetStart;
                tbCurrentInfo.Text =
                    (string.Format("{0}期 距离StartTime时间还有{1}", _playDataInfo.LastPlayData.IsusesName,
                                   FormatTime(_playDataInfo.LastPlayData.StartTime - DateTime.Now)));
                return;
            }
            if (DateTime.Now < _playDataInfo.LastPlayData.AheadEndTime)
            {
                CurrentBonusStatus = BonusStatus.StartBet;
                tbCurrentInfo.Text =
                    (string.Format("{0}期 距离AheadEndTime时间还有{1}", _playDataInfo.LastPlayData.IsusesName,
                                   FormatTime(_playDataInfo.LastPlayData.AheadEndTime - DateTime.Now)));
                return;
            }
            if (DateTime.Now < _playDataInfo.LastPlayData.EndTime)
            {
                CurrentBonusStatus = BonusStatus.EndBet;
                tbCurrentInfo.Text =
                    (string.Format("{0}期 距离EndTime时间还有{1}", _playDataInfo.LastPlayData.IsusesName,
                                   FormatTime(_playDataInfo.LastPlayData.EndTime - DateTime.Now)));
                return;
            }
            if (string.IsNullOrEmpty(_playDataInfo.LastPlayData.BonusNumber))
            {
                tbLastInfo.Text = (string.Format("{0}", _playDataInfo.LastPlayData.IsusesName));
                if (!string.IsNullOrEmpty(WinNumber))
                {
                    WinNumber = "";
                    if (LoadData.Visibility != null)
                    {
                        LoadData.Visibility = Visibility.Visible;
                    }
                    if (pompon.Visibility != Visibility.Collapsed)
                    {
                        pompon.Visibility = Visibility.Collapsed;
                    }
                    for (int i = 0; i < pompon.Children.Count; i++)
                    {
                        var canvas = (Canvas) pompon.Children[i];
                        canvas.SetValue(Canvas.LeftProperty, -80.0);
                    }
                    SetStoryboard();
                }
            }
            else
            {
                //向右滚动到位效果
                if (string.IsNullOrEmpty(WinNumber))
                {
                    WinNumber = _playDataInfo.LastPlayData.BonusNumber;
                    tbLastInfo.Text = (string.Format("{0}", _playDataInfo.LastPlayData.IsusesName));
                    if (LoadData.Visibility != Visibility.Collapsed)
                    {
                        LoadData.Visibility = Visibility.Collapsed;
                    }
                    if (pompon.Visibility != null)
                    {
                        pompon.Visibility = Visibility.Visible;
                    }

                    _int1 = WinNumber.Length - 1;
                    _int2 = WinNumber.Length;
                    if (_int1 > 0)
                    {
                        SetStoryboard2(_int1);
                    }
                    CreateLastDataUIElement();
                }
            }
            if (DateTime.Now < _playDataInfo.CurrentPlayData.StartTime)
            {
                CurrentBonusStatus = BonusStatus.NetStart;
                tbCurrentInfo.Text =
                    (string.Format("{0}期 距离StartTime时间还有{1}", _playDataInfo.CurrentPlayData.IsusesName,
                                   FormatTime(_playDataInfo.CurrentPlayData.StartTime - DateTime.Now)));
                return;
            }
            if (DateTime.Now < _playDataInfo.CurrentPlayData.AheadEndTime)
            {
                CurrentBonusStatus = BonusStatus.StartBet;
                TimeSpan timeSpan = _playDataInfo.CurrentPlayData.AheadEndTime - DateTime.Now;
                tbCurrentInfo.Text =
                    (string.Format("{0}期 距离AheadEndTime时间还有{1}", _playDataInfo.CurrentPlayData.IsusesName,
                                   FormatTime(timeSpan)));
                double num = Math.Round(double.Parse(timeSpan.TotalSeconds.ToString()), 0);
                if (DataCache.RealSound && num >= 0.0 && num < 8.0 && num%2.0 == 0.0)
                {
                    msgCountdown.Stop();
                    msgCountdown.Play();
                    return;
                }
            }
            else
            {
                if (DateTime.Now < _playDataInfo.CurrentPlayData.EndTime)
                {
                    CurrentBonusStatus = BonusStatus.EndBet;
                    tbCurrentInfo.Text =
                        (string.Format("{0}期 距离EndTime时间还有{1}", _playDataInfo.CurrentPlayData.IsusesName,
                                       FormatTime(_playDataInfo.CurrentPlayData.EndTime - DateTime.Now)));
                    return;
                }
                if (_eventHandler1 != null)
                {
                    _eventHandler1.Invoke(this, null);
                }
            }
        }

        private string FormatString(string text)
        {
            var stringBuilder = new StringBuilder();
            for (int i = 0; i < text.Length; i++)
            {
                if (i != text.Length - 1)
                {
                    stringBuilder.Append(text.ToCharArray()[i] + " ");
                }
                else
                {
                    stringBuilder.Append(text.ToCharArray()[i]);
                }
            }
            return stringBuilder.ToString().Trim();
        }

        /// <summary>
        /// 模拟中奖号码
        /// </summary>
        public void CreateLastDataUIElement()
        {
            if (_playDataInfo != null)
            {
                RecentlyLottery.Children.Clear();
                int num = 0;
                if (_playDataInfo.LastOpenPlayData.Count > 4)
                {
                    num = 2;
                }
                else
                {
                    if (_playDataInfo.LastOpenPlayData.Count == 4)
                    {
                        num = 1;
                    }
                }
                int num2 = 0;
                //\\?
                //for (int i = num; i < _playDataInfo.LastOpenPlayData.Count; i++)
                //{
                for (int i = num; i < 3; i++)
                {
                    var canvas = new Canvas();
                    canvas.Width = (320.0);
                    canvas.Height = (20.0);
                    var textBlock = new TextBlock();
                    textBlock.Width = (80.0);
                    textBlock.Height = (16.0);
                    textBlock.Foreground = (new SolidColorBrush((Color) base.Resources["FontColor"]));
                    textBlock.FontSize = (12.0);
                    //\\?
                    //textBlock.Text = (_playDataInfo.LastOpenPlayData[i].IsusesName);
                    textBlock.Text = "20120808";
                    textBlock.TextAlignment = (0);
                    textBlock.SetValue(Canvas.LeftProperty, 6.0);
                    canvas.Children.Add(textBlock);
                    //\\?
                    //string[] array = FormatString(_playDataInfo.LastOpenPlayData[i].BonusNumber).Split(new[]
                    //                                                                                     {
                    //                                                                                         ','
                    //                                                                                     });
                    string[] array = FormatString("888").Split(' ');
                    var canvas2 = new Canvas();
                    canvas2.Width = ((array.Length*23));
                    for (int j = 0; j < array.Length; j++)
                    {
                        var ellipse = new Ellipse();
                        ellipse.Width = (22.0);
                        ellipse.Height = (22.0);
                        ellipse.Fill = (new SolidColorBrush((Color) base.Resources["BallColor1"]));
                        ellipse.SetValue(Canvas.LeftProperty, j*24.0);
                        ellipse.SetValue(Canvas.TopProperty, -3.0);
                        canvas2.Children.Add(ellipse);
                        var ellipse2 = new Ellipse();
                        ellipse2.Style = ((Style) base.Resources["EllipseStyle1"]);
                        ellipse2.SetValue(Canvas.LeftProperty, j*24.0);
                        ellipse2.SetValue(Canvas.TopProperty, -3.0);
                        canvas2.Children.Add(ellipse2);
                        var textBlock2 = new TextBlock();
                        textBlock2.Text = (array[j]);
                        textBlock2.Width = (22.0);
                        textBlock2.TextAlignment = (0);
                        textBlock2.FontSize = (11.0);
                        textBlock2.Foreground = (new SolidColorBrush(Colors.White));
                        textBlock2.SetValue(Canvas.LeftProperty, j*24.0);
                        textBlock2.SetValue(Canvas.TopProperty, 0.0);
                        canvas2.Children.Add(textBlock2);
                    }
                    var grid = new Grid();
                    grid.Width = (120.0);
                    grid.Height = (22.0);
                    canvas2.HorizontalAlignment = HorizontalAlignment.Right;
                    grid.Children.Add(canvas2);
                    grid.SetValue(Canvas.LeftProperty, 120.0);
                    canvas.Children.Add(grid);
                    var textBlock3 = new TextBlock();
                    textBlock3.Width = (80.0);
                    textBlock3.Height = (16.0);
                    textBlock3.Foreground = (new SolidColorBrush((Color) base.Resources["FontColor"]));
                    textBlock3.FontSize = (12.0);
                    //\\?
                    //textBlock3.Text = (_playDataInfo.LastOpenPlayData[i].ZXType);

                    textBlock3.Text = ("大双");
                    textBlock3.TextAlignment = (0);
                    textBlock3.SetValue(Canvas.LeftProperty, 250.0);
                    canvas.Children.Add(textBlock3);
                    canvas.SetValue(Canvas.TopProperty, num2*32.0 + 7.0);
                    RecentlyLottery.Children.Add(canvas);
                    num2++;
                }
            }
        }

        private string FormatTime(TimeSpan timeSpan)
        {
            if (timeSpan.TotalHours > 1.0)
            {
                return string.Format("hh:mm:ss", timeSpan);
            }
            if (timeSpan.TotalMinutes > 1.0)
            {
                return string.Format("hh:mm:ss", timeSpan);
            }
            return string.Format("hh:mm:ss", timeSpan);
        }

        private void SetPomponChildrenProperty()
        {
            for (int i = 0; i < pompon.Children.Count; i++)
            {
                var canvas = (Canvas) pompon.Children[i];
                canvas.SetValue(Canvas.LeftProperty, -80.0);
            }
            pompon.Visibility = Visibility.Collapsed;
            _dispatcherTimer = new DispatcherTimer();
            _dispatcherTimer.Interval = (new TimeSpan(1000L));
            _dispatcherTimer.Tick += (_dispatcherTimer_Tick);
        }

        private void SetStoryboard2(int num)
        {
            if (num >= 0 && num < WinNumber.Length)
            {
                var canvas = (Canvas) pompon.Children[num];
                var textBlock = (TextBlock) canvas.Children[1]
                    ;
                textBlock.Text = (WinNumber.Substring(num, 1));
                if (_double1 < 0.0)
                {
                    _double1 = 50.0 - _int2*6.5;
                }
                if (_double2 < 0.0)
                {
                    _double2 = (pompon.Width - ((_int2 - 1)*_double1 + canvas.Width*_int2))/2.0;
                }
                var doubleAnimation = new DoubleAnimation();
                doubleAnimation.From = (-80.0);
                doubleAnimation.To = (_double2 + num*canvas.Width + _double1*num);
                doubleAnimation.Duration = (new Duration(TimeSpan.FromMilliseconds(((num + 1)*120))));
                Storyboard.SetTarget(doubleAnimation, canvas);
                Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(Canvas.LeftProperty));
                var doubleAnimation2 = new DoubleAnimation();
                doubleAnimation2.From = (-1080.0);
                doubleAnimation2.To = (((num + 1)*360));
                doubleAnimation2.Duration = (new Duration(TimeSpan.FromMilliseconds(((num + 1)*170))));
                Storyboard.SetTarget(doubleAnimation2, canvas);
                Storyboard.SetTargetProperty(doubleAnimation2,
                                             new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Rotation)",
                                                              new object[0]));
                var storyboard = new Storyboard();
                storyboard.Completed += (storyboard_Completed);
                storyboard.Children.Add(doubleAnimation);
                storyboard.Children.Add(doubleAnimation2);
                storyboard.Begin();
            }
        }

        private void SetStoryboard()
        {
            if (_storyboard == null)
            {
                _storyboard = new Storyboard();
                for (int i = 0; i < LoadData.Children.Count; i++)
                {
                    var canvas = (Canvas) LoadData.Children[i];
                    var ellipseGeometry = new EllipseGeometry();
                    ellipseGeometry.RadiusX = (18.0);
                    ellipseGeometry.RadiusY = (18.0);
                    ellipseGeometry.Center = (new Point(18.0, 18.0));
                    canvas.Clip = (ellipseGeometry);
                    var canvas2 = (Canvas) canvas.Children[0];
                    var textBlock = (TextBlock) canvas.Children[1];
                    if (_double1 < 0.0)
                    {
                        _double1 = 17.5;
                    }
                    if (_double2 < 0.0)
                    {
                        _double2 = (pompon.Width - (4.0*_double1 + canvas.Width*5.0))/2.0;
                    }
                    canvas.SetValue(Canvas.LeftProperty, _double2 + i*canvas.Width + _double1*i);
                    var doubleAnimation = new DoubleAnimation();
                    doubleAnimation.From = (0.0);
                    doubleAnimation.To = (-144.0);
                    doubleAnimation.Duration = (new Duration(TimeSpan.FromMilliseconds(2000.0)));
                    Storyboard.SetTarget(doubleAnimation, canvas2);
                    Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(Canvas.TopProperty));
                    var doubleAnimation2 = new DoubleAnimation();
                    doubleAnimation2.From = (-120.0);
                    doubleAnimation2.To = (36.0);
                    doubleAnimation2.Duration = (new Duration(TimeSpan.FromMilliseconds(2000.0)));
                    Storyboard.SetTarget(doubleAnimation2, textBlock);
                    Storyboard.SetTargetProperty(doubleAnimation2, new PropertyPath(Canvas.TopProperty));
                    _storyboard.Children.Add(doubleAnimation);
                    _storyboard.Children.Add(doubleAnimation2);
                }
                _storyboard.Completed += (_storyboard_Completed);
                _storyboard.Begin();
                _double1 = -1.0;
                _double2 = -1.0;
                return;
            }
            _storyboard.Begin();
        }

        private void _storyboard_Completed(object obj, EventArgs eventArgs)
        {
            _storyboard.Begin();
            //if (LoadData.Visibility == null)
            //{
            //    _storyboard.Begin();
            //}
        }

        private void storyboard_Completed(object obj, EventArgs eventArgs)
        {
            _int1--;
            if (_int1 == 0 && DataCache.RealSound)
            {
                msgComplete.Stop();
                msgComplete.Play();
            }
            SetStoryboard2(_int1);
        }
    }
}